package com.jsb.laundry.controller;

import com.alibaba.fastjson.JSONObject;
import com.jsb.laundry.common.config.Constant;
import com.jsb.laundry.common.enums.ReturnStatusCode;
import com.jsb.laundry.common.response.WebResponse;
import com.jsb.laundry.common.utils.JSBUtil;
import com.jsb.laundry.dao.WithdrawalDao;
import com.jsb.laundry.model.dto.RiderWithdrawalLogRequest;
import com.jsb.laundry.model.entity.BankInfo;
import com.jsb.laundry.model.entity.Withdrawals;
import com.jsb.laundry.model.vo.WithdrawalUserVo;
import com.jsb.laundry.service.RiderUserInfoService;
import com.jsb.laundry.service.WithdrawalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("withdrawal")
public class WithdrawalController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WithdrawalController.class);
    @Resource
    private RiderUserInfoService userInfoService;
    @Resource
    private WithdrawalService withdrawalService;
    @Resource
    private WithdrawalDao withdrawalDao;


    @PostMapping("withdrawalIngVo")
    public WebResponse withdrawalIngVo(@RequestBody JSONObject object){
        String money = object.getString("money");
        if (JSBUtil.isEmptyString(money)){
            return WebResponse.success(ReturnStatusCode.MONEY_NO.getCode(), ReturnStatusCode.MONEY_NO.getMsg());
        }
        double parseDouble = Double.parseDouble(money);
        double moneytodb = parseDouble * Constant.withdrawal_rate;
        String formattedNumber = String.format("%.2f", moneytodb);
        HashMap map =new HashMap();
        map.put("moneytodb","￥"+formattedNumber);
        double baifen = Constant.withdrawal_rate * 100;
        map.put("withdrawalRate",(int)baifen+"%");
        return WebResponse.successData(map);
    }

    @PostMapping("withdrawalIng")
    @Transactional
    public WebResponse withdrawalIng(@RequestBody RiderWithdrawalLogRequest riderWithdrawalLogRequest) {
        WithdrawalUserVo byUserMoney = userInfoService.findByUserMoney(riderWithdrawalLogRequest.getUserId());
        String deposit = byUserMoney.getDeposit();
        //本账号可提现金额
        int deposit1 = Integer.parseInt(deposit);
        //用户申请提现金额
        double money = riderWithdrawalLogRequest.getMoney()* 100;//提现金额
        double mo = money +money * Constant.withdrawal_rate;//提现收取6%的手续费
        //TODO 计算扣除手续费的提现到账金额  余额-手续费-提现金额
        double userWithdrawalMoneyDet = money - mo;
        if (mo>deposit1) {
            return WebResponse.success(ReturnStatusCode.WITHDRAWAL_AMOUNT_EXCEEDS.getCode(), ReturnStatusCode.WITHDRAWAL_AMOUNT_EXCEEDS.getMsg());
        }

        //关联的单号
        String withdrawalNo = "R" + JSBUtil.generateLongOrderNO();
        //提现表添加记录
//        int i = userInfoService.addWithdrawalLog(uuid, riderWithdrawalLogRequest.getUserId(), userWithdrawalMoney);
        Withdrawals withdrawals = new Withdrawals();
        withdrawals.setWithdrawalNo(withdrawalNo);//提现编号
        withdrawals.setUserId(riderWithdrawalLogRequest.getUserId());//用户id
        withdrawals.setBatchNo(null);//微信
        withdrawals.setAppId(null);//微信
        withdrawals.setMchId(null);//微信
        withdrawals.setOpenId(null);//微信
        withdrawals.setAmount((int) money);//提现金额
        withdrawals.setSource("2");//来源
        withdrawals.setWithdrawalType("2");//提现类型
        //根据用户id查询用户银行卡信息
        BankInfo byRiderBankInfo = withdrawalDao.findByRiderBankInfo(riderWithdrawalLogRequest.getUserId());

        withdrawals.setBankName(byRiderBankInfo.getBankName());//银行卡名称

        withdrawals.setBankCardNumber(byRiderBankInfo.getBankCardId());//银行卡号
        double commission_amount = money * Constant.withdrawal_rate;
        withdrawals.setCommissionAmount((int) commission_amount);//提现手续费金额

        withdrawals.setCommissionRate(Constant.withdrawal_rate);//手续费率

        withdrawals.setReqData("");//请求数据
        withdrawals.setResData("");//返回数据
        int i = userInfoService.addWithdrawalsLog(withdrawals);

        withdrawalService.splitSettled(withdrawalNo, riderWithdrawalLogRequest.getUserId());

        return WebResponse.successData(i);
    }

    @PostMapping("byDeposit")
    public WebResponse byDeposit(@RequestBody RiderWithdrawalLogRequest riderWithdrawalLogRequest) {
        int byDeposit = withdrawalDao.findByDeposit(riderWithdrawalLogRequest.getUserId());
        String money = JSBUtil.turnDoubleStrNew(byDeposit);
        return WebResponse.successData(money);
    }
}
